﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LandandSurvey
{
    public partial class EditBookPlan : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                if ( !HasPermission() )
                {
                    Response.Redirect( "ErrorPermission.aspx?returnurl=EditOther.aspx?id=" + Request[ "id" ] );
                }
                else
                {
                    using ( landsdbEntities container = new landsdbEntities() )
                    {
                        int id = Convert.ToInt32( Request[ "id" ] );

                        var otherplan = ( from i in container.BookPlans
                                          where i.Id == id
                                          select i ).FirstOrDefault();

                        if ( otherplan != null )
                        {
                            txtAcreage.Text = otherplan.Acreage;
                            txtCTFolio.Text = otherplan.CTFolio;
                            txtCTVolume.Text = otherplan.CTVolume;
                            txtDateSignedDirector.Text = otherplan.DateSignedDirector;
                            txtDateSignedSurveyor.Text = otherplan.DateSignedSurveyor;
                            txtLSNumber.Text = otherplan.LandSNumber;
                            txtName.Text = otherplan.Name;
                            txtBookNo.Text = otherplan.BookNo;
                            txtFolio.Text = otherplan.Folio;
                            txtRemarks.Text = otherplan.Remarks;
                            txtRPOFolio.Text = otherplan.RPOFolio;
                            txtRPOVolume.Text = otherplan.RPOVolume;
                            txtSurveyor.Text = otherplan.Surveyor;
                            txtSurveoyOrderNo.Text = otherplan.SurveyOrderNumber;
                            txtWardRefCassini.Text = otherplan.WardRefCassini;
                            txtWard.Text = otherplan.Ward;
                            txtWardRefCassini.Text = otherplan.WardRefCassini;
                            txtWardRefUTM.Text = otherplan.WardRefUTM;
                            txtDeedNumber.Text = otherplan.DeedNumber;
                        }
                    }
                }
            }
        }

        public bool HasPermission()
        {
            if ( Session[ "user.loggedin" ] == null )
            {
                return false;
            }
            if ( Session[ "user.loggedin" ].ToString() == "true" )
            {
                landsdbEntities container = new landsdbEntities();
                string username = Session[ "user.name" ].ToString();
                AppUser user = ( from i in container.AppUsers
                                 where i.UserName == username
                                 select i ).ToList().FirstOrDefault();
                if ( user != null )
                {
                    if ( user.UploadFiles == true )
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        protected void btnSave_Click( object sender, EventArgs e )
        {
            using ( landsdbEntities context = new landsdbEntities() )
            {
                int id = Convert.ToInt16( Request[ "id" ] );

                var otherplan = ( from i in context.BookPlans
                                  where i.Id == id
                                  select i ).FirstOrDefault();

                otherplan.Acreage = txtAcreage.Text;
                otherplan.CTFolio = txtCTFolio.Text;
                otherplan.CTVolume = txtCTVolume.Text;
                otherplan.DateSignedDirector = txtDateSignedDirector.Text;
                otherplan.DateSignedSurveyor = txtDateSignedSurveyor.Text;
                otherplan.LandSNumber = txtLSNumber.Text;
                otherplan.Name = txtName.Text;
                otherplan.BookNo = txtBookNo.Text;
                otherplan.Folio = txtFolio.Text;
                otherplan.Remarks = txtRemarks.Text;
                otherplan.RPOFolio = txtRPOFolio.Text;
                otherplan.RPOVolume = txtRPOVolume.Text;
                otherplan.Surveyor = txtSurveyor.Text;
                otherplan.SurveyOrderNumber = txtSurveoyOrderNo.Text;
                otherplan.WardRefCassini = txtWardRefCassini.Text;
                otherplan.Ward = txtWard.Text;
                otherplan.WardRefCassini = txtWardRefCassini.Text;
                otherplan.WardRefUTM = txtWardRefUTM.Text;
                otherplan.DeedNumber = txtDeedNumber.Text;

                context.BookPlans.ApplyCurrentValues( otherplan );
                context.SaveChanges();
            }
        }
    }
}